﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using database;
using receipt;

namespace Accounting
{
    public partial class Form2 : Form
    {
        dbase process = new dbase();
        dbase db = new dbase();
        dbase dbTuition = new dbase();
        DataTable students = new DataTable();
        DataTable fees = new DataTable();
        dbase tmpDB = new dbase();
        receiptClass rc = new receiptClass();
        Form1 frm;
        public Form2(Form1 frm1)
        {
            InitializeComponent();
            frm = frm1;
            this.FormClosed += new FormClosedEventHandler(Form2_FormClosed);
            this.Load += new EventHandler(Form2_Load);
            radioButton1.CheckedChanged += new EventHandler(radioButton1_CheckedChanged);
            button1.Click += new EventHandler(button1_Click);
            db.process_completed += new EventHandler(db_process_completed);
            buttonCancel.Click += new EventHandler(buttonCancel_Click);
            buttonCancel2.Click += new EventHandler(buttonCancel2_Click);
            comboBoxGrade.TextChanged += new EventHandler(comboBoxGrade_TextChanged);
            dbTuition.process_completed += new EventHandler(dbTuition_process_completed);
            buttonSave.Click += new EventHandler(buttonSave_Click);
            buttonUpdate.Click += new EventHandler(buttonUpdate_Click);
            buttonRefresh.Click += new EventHandler(buttonRefresh_Click);
            buttonSearch.Click += new EventHandler(buttonSearch_Click);
        }

        void buttonSearch_Click(object sender, EventArgs e)
        {
            if (textBoxID.Text != "")
            {
                searchID();
            }
        }

        void buttonRefresh_Click(object sender, EventArgs e)
        {
            db.GenerateConnection("SELECT * FROM payment_tbl");
        }

        void buttonUpdate_Click(object sender, EventArgs e)
        {
            int tmp = int.Parse(textBoxBalance.Text) - int.Parse(textBoxPayment.Text);
            amt_paid += int.Parse(textBoxPayment.Text);

            rc.curBal = int.Parse(textBoxBalance.Text);
            rc.grade = comboBox1.Text;
            rc.name = textBoxName.Text;
            rc.payment = int.Parse(textBoxPayment.Text);
            rc.id = textBoxID.Text;
            rc.print();

            process.connectionDatabase("UPDATE payment_tbl SET amt_paid='" + amt_paid + "',remaining_bal='" + tmp + "',grade='" + comboBox1.Text + "' WHERE id='" + textBoxID.Text + "'");
            groupBox4.Enabled = false;
            textBoxBalance.Text = "";
            textBoxName.Text = "";
            textBoxPayment.Text = "";
            textBoxID.Text = "";
            MessageBox.Show("Updated Successfully");
            db.GenerateConnection("SELECT * FROM payment_tbl");
        }

        void buttonSave_Click(object sender, EventArgs e)
        {
            bool pass = false;
            process.connectionDatabase("SELECT * FROM payment_tbl WHERE id='"+textBoxID2.Text+"'");
            if (process.data().Rows.Count <= 0)
            {
                pass = true;
            }
            if (pass == true)
            {
                try
                {
                    if (comboBoxGrade.Text != "" && textBoxID2.Text != ""
                        && textBoxLastName.Text != "" && textBoxFirstName.Text != "" 
                        && textBoxMiddleName.Text != ""
                        && textBoxPayment2.Text != ""
                        && int.Parse(textBox1.Text) >= int.Parse(textBoxPayment2.Text))
                    {
                        string tmpName = textBoxLastName.Text + "~" + textBoxFirstName.Text + "~" + textBoxMiddleName.Text;

                        int bal = int.Parse(textBox1.Text) - int.Parse(textBoxPayment2.Text);
                        process.connectionDatabase("INSERT INTO payment_tbl(id,fname,amt_paid,remaining_bal,grade) VALUES('" +
                            textBoxID2.Text + "','" + tmpName + "','" + textBoxPayment2.Text + "','" + bal + "','" + comboBoxGrade.Text + "')");
                        db.GenerateConnection("SELECT * FROM payment_tbl");

                        rc.curBal = int.Parse(textBox1.Text);
                        rc.grade = comboBoxGrade.Text;
                        rc.name = textBoxLastName.Text + ", " + textBoxFirstName.Text + ", " + textBoxMiddleName.Text;
                        rc.payment = int.Parse(textBoxPayment2.Text);
                        rc.id = textBoxID2.Text;
                        rc.print();

                        textBoxID2.Text = "";
                        textBoxLastName.Text = "";
                        textBoxFirstName.Text = "";
                        textBoxMiddleName.Text = "";
                        textBoxPayment2.Text = "";
                        comboBoxGrade.Text = "";
                        textBox1.Text = "";

                        MessageBox.Show("Student Account Save.");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
            else
            {
                MessageBox.Show("ID Number is already in use.");
            }
        }

        void dbTuition_process_completed(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                EventHandler eh = new EventHandler(dbTuition_process_completed);
                this.Invoke(eh, new object[] { dbTuition.data() });
                return;
            }
            fees = dbTuition.data();
        }

        void comboBoxGrade_TextChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < fees.Rows.Count; i++)
            {
                if (comboBoxGrade.Text == fees.Rows[i]["grade"].ToString())
                {
                    textBoxID2.Text = (students.Rows.Count + 1).ToString();
                    textBox1.Text = fees.Rows[i]["tuition"].ToString();
                    break;
                }
            }
        }

        void buttonCancel2_Click(object sender, EventArgs e)
        {
            textBoxID2.Text = "";
            textBoxLastName.Text = "";
            textBoxFirstName.Text = "";
            textBoxMiddleName.Text = "";
            textBoxPayment2.Text = "";
            comboBoxGrade.Text = "";
            textBox1.Text = "";
        }

        void buttonCancel_Click(object sender, EventArgs e)
        {
            textBoxID.Text = "";
        }

        int amt_paid = 0;
        void searchID()
        {
            for (int i = 0; i < students.Rows.Count; i++)
            {
                if (students.Rows[i]["id"].ToString() == textBoxID.Text && students.Rows[i]["active"].ToString() == "1")
                {
                    tmpDB.connectionDatabase("SELECT * FROM student_profile_tbl WHERE id='" + students.Rows[i]["id"].ToString() + "'");
                    groupBox4.Enabled = true;
                    comboBox1.Text = tmpDB.data().Rows[0]["grade_lvl"].ToString();

                    string [] tmpName = students.Rows[i]["fname"].ToString().Split('~'); 
                    textBoxName.Text = tmpName[0] + " " + tmpName[1] + " " + tmpName[2];

                    tmpDB.connectionDatabase("SELECT * FROM student_profile_tbl WHERE id='"+students.Rows[i]["id"].ToString()+"'");

                    if (students.Rows[i]["grade"].ToString() != tmpDB.data().Rows[0]["grade_lvl"].ToString())
                    {
                        int fee = 0;
                        for (int a = 0; a < fees.Rows.Count; a++)
                        {
                            if (comboBox1.Text == tmpDB.data().Rows[0]["grade_lvl"].ToString())
                            {
                                fee = int.Parse(fees.Rows[i]["tuition"].ToString());
                                break;
                            }
                        }

                        textBoxBalance.Text = (int.Parse(students.Rows[i]["remaining_bal"].ToString()) + fee).ToString();
                    }
                    else
                    {
                        textBoxBalance.Text = students.Rows[i]["remaining_bal"].ToString();
                    }
                    amt_paid = int.Parse(students.Rows[i]["amt_paid"].ToString());
                    break;
                }
                else
                {
                    groupBox4.Enabled = false;
                    textBoxBalance.Text = "";
                    textBoxName.Text = "";
                    textBoxPayment.Text = "";
                }
            }
        }

        void db_process_completed(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                EventHandler eh = new EventHandler(db_process_completed);
                this.Invoke(eh, new object[] { db.data() });
                return;
            }
            students = db.data();
        }

        void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                groupBox2.Enabled = true;
                groupBox3.Enabled = false;
            }
            else
            {
                groupBox3.Enabled = true;
                groupBox2.Enabled = false;
            }
        }

        void Form2_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            db.GenerateConnection("SELECT * FROM payment_tbl");
            dbTuition.GenerateConnection("SELECT * FROM tuition_fees");
        }

        void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            frm.active = false;
        }
    }
}
